package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.entity.UpdateArticleMessage;
import com.heima.behavior.service.IReadService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import com.sun.rowset.CachedRowSetImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ReadServiceImpl implements IReadService {
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public ResponseResult saveRead(ReadBehaviorDto dto) {
        ApReadBehavior  behavior=new ApReadBehavior();
        User user = AppThreadLocalUtil.get();
        if(user==null){
            behavior.setEquipmentId(dto.getEquipmentId());
        }else{
            behavior.setUserId(user.getUserId());
        }
        behavior.setArticleId(dto.getArticleId());
        behavior.setCount(1);
        behavior.setReadDuration(dto.getReadDuration());
        behavior.setPercentage(dto.getPercentage());
        behavior.setLoadDuration(dto.getLoadDuration());
        behavior.setCreatedTime(new Date());
        //查询是否有过历史记录
        Query query=new Query();
        query.addCriteria(Criteria.where("articleId").is(dto.getArticleId()));
        if(user==null){
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }else{
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        }
        ApReadBehavior one = mongoTemplate.findOne(query, ApReadBehavior.class);
        if(one==null){
            mongoTemplate.save(behavior);
        }else{
            Update update=new Update();
            update.inc("count");
            update.set("readDuration",dto.getReadDuration());
            update.set("percentage",dto.getPercentage());
            update.set("loadDuration",dto.getLoadDuration());
            update.set("updatedTime",new Date());
            mongoTemplate.updateFirst(query,update,ApReadBehavior.class);
        }
        // 发送阅读消息到kafka
        UpdateArticleMessage message = new UpdateArticleMessage();
        message.setType(0);
        message.setArticleId(dto.getArticleId());
        message.setAdd(1);
        String value = JSON.toJSONString(message);
        kafkaTemplate.send(ReadTopic, value);
        return ResponseResult.okResult();
    }

    @Value("${topic.read_behavior}")
    private String ReadTopic;

}
